/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"cmdTools/kafka"
	"fmt"
	"github.com/spf13/cobra"
)

var (
	server            []string
	topic             []string
	key               string
	value             string
	ReplicaAssignment string
	consumerGroup     []string
	numPartition      int
	duplicate         int
	frist             bool
	startTime         string
)

// kafkaCmd represents the kafka command
var kafkaCmd = &cobra.Command{
	Use:   "kafka",
	Short: "Common kafka operations",
	Long:  ``,
	/*	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("kafka called ", server, topic, args)

	},*/
}

var productMsg = &cobra.Command{
	Use:     "producter",
	Short:   "Send a message to the topic",
	Long:    `Send a message to the topic`,
	Example: "cmdTools kafka -s localhost:9092 -t topic producter -k k1 -v v1",
	Run: func(cmd *cobra.Command, args []string) {
		kafka.CAKafkaer.KafkaInit(server)
		kafka.CAKafkaer.CreateClient()
		kafka.CAKafkaer.SendMsgToTopic(topic[0], key, value)
	},
}

var descTopic = &cobra.Command{
	Use:     "describetopic",
	Aliases: []string{"dt", "desctopic", "desct"},
	Short:   "Show topic details",
	Long:    `Show topic details`,
	Example: "cmdTools kafka -s localhost:9092 desctopic -t topic",
	Run: func(cmd *cobra.Command, args []string) {
		kafka.CAKafkaer.KafkaInit(server)
		kafka.CAKafkaer.CreateClusterAdmin()
		kafka.CAKafkaer.CreateClient()
		kafka.CAKafkaer.DescribeTopic(topic)
	},
}

var UpdatePartitionReassignments = &cobra.Command{
	Use:     "updatepartition",
	Aliases: []string{"up", "updatep"},
	Short:   "Update Partition Reassignments",
	Long:    `Update the partition distribution to balance all nodes`,
	Example: "cmdTools kafka -s localhost:9092 up   -t topic",
	Run: func(cmd *cobra.Command, args []string) {
		kafka.CAKafkaer.KafkaInit(server)
		kafka.CAKafkaer.CreateClusterAdmin()
		kafka.CAKafkaer.CreateClient()
		kafka.CAKafkaer.UpdatePartitionReassignments(topic[0], ReplicaAssignment)
	},
}

var expandPartitionsForAll = &cobra.Command{
	Use:     "expandpartitionsforall",
	Aliases: []string{"epa", "expandpa"},
	Short:   "Extend all partitions",
	Long:    `Extend all topic partitions to the number of brokers, and reallocate balanced nodes`,
	Example: "cmdTools kafka -s localhost:9092 expandall",
	Run: func(cmd *cobra.Command, args []string) {
		kafka.CAKafkaer.KafkaInit(server)
		kafka.CAKafkaer.CreateClusterAdmin()
		kafka.CAKafkaer.ExpandPartitionsForAll()
	},
}

var CreateTopic = &cobra.Command{
	Use:     "createtopic",
	Aliases: []string{"ct", "createt"},
	Short:   "Create topic",
	Long:    `Creates topic`,
	Example: "cmdTools kafka -s localhost:9092 createt -t topic  -n 6  -d 2 ",
	Run: func(cmd *cobra.Command, args []string) {
		kafka.CAKafkaer.KafkaInit(server)
		kafka.CAKafkaer.CreateClusterAdmin()
		kafka.CAKafkaer.CreateTopic(topic[0], numPartition, duplicate)
	},
}

var DeleteTopic = &cobra.Command{
	Use:     "deletetopic",
	Aliases: []string{"deltopic", "dtopic"},
	Short:   "Delete topic",
	Long:    `Delete topic`,
	Example: "cmdTools kafka -s localhost:9092 deltopic -t topic",
	Run: func(cmd *cobra.Command, args []string) {
		kafka.CAKafkaer.KafkaInit(server)
		kafka.CAKafkaer.CreateClusterAdmin()
		kafka.CAKafkaer.DeleteTopic(topic[0])
	},
}

var CreatePartitions = &cobra.Command{
	Use:     "createpartition",
	Aliases: []string{"cp", "createp"},
	Short:   "Create partitions",
	Long:    `Creates a partition based on the specified partition mapping`,
	Example: "cmdTools kafka -s localhost:9092 createp -t topic  -n 6 -r [[0],[0],[0]] (new partition map or nil)",
	Run: func(cmd *cobra.Command, args []string) {
		kafka.CAKafkaer.KafkaInit(server)
		kafka.CAKafkaer.CreateClusterAdmin()
		kafka.CAKafkaer.CreatePartitions(topic[0], ReplicaAssignment, numPartition)
	},
}

var DescribeConsumerGroup = &cobra.Command{
	Use:     "describeconsumergroup ",
	Aliases: []string{"dcg", "descdcg"},
	Short:   "Display consumer group details",
	Long:    `Display consumer group details`,
	Example: "cmdTools kafka -s localhost:9092 desccg   -c cg1,cg2",
	Run: func(cmd *cobra.Command, args []string) {
		kafka.CAKafkaer.KafkaInit(server)
		kafka.CAKafkaer.CreateClusterAdmin()
		kafka.CAKafkaer.CreateClient()
		kafka.CAKafkaer.DescribeConsumerGroup(consumerGroup)
	},
}

var ConsumerTopic = &cobra.Command{
	Use:     "consumer",
	Short:   "Consumer topic",
	Long:    `Consume a topic using the cmdToolsGroup consumption group,`,
	Example: "cmdTools kafka -s localhost:9092 consumer -t topic -f",
	Run: func(cmd *cobra.Command, args []string) {
		kafka.CAKafkaer.KafkaInit(server)
		kafka.CAKafkaer.CreateClusterAdmin()
		kafka.CAKafkaer.CreateClient()
		fmt.Println("startTime: ", startTime)
		kafka.CAKafkaer.ConsumerTopic(topic, frist, startTime)
	},
}

func init() {
	kafkaCmd.PersistentFlags().StringSliceVarP(&server, "server", "s", []string{"localhost:9092"}, "kafka address: localhost:9092,localhost:9093")
	kafkaCmd.PersistentFlags().StringSliceVarP(&topic, "topic", "t", []string{}, "kafka topic: t1,t2")

	productMsg.Flags().StringVarP(&key, "key", "k", "key", "msg key")
	productMsg.Flags().StringVarP(&value, "value", "v", "value", "msg value")
	productMsg.Flags().StringSliceVarP(&topic, "topic", "t", []string{}, "kafka topic: t1,t2")
	_ = productMsg.MarkFlagRequired("topic")
	_ = productMsg.MarkFlagRequired("value")
	kafkaCmd.AddCommand(productMsg)

	descTopic.Flags().StringSliceVarP(&topic, "topic", "t", []string{}, "kafka topic: t1,t2")
	kafkaCmd.AddCommand(descTopic)

	UpdatePartitionReassignments.Flags().StringVarP(&ReplicaAssignment, "ReplicaAssignment", "r", "[]", "ReplicaAssignment: [[1,2],[0,1]]")
	UpdatePartitionReassignments.Flags().StringSliceVarP(&topic, "topic", "t", []string{}, "kafka topic: t1,t2")
	_ = UpdatePartitionReassignments.MarkFlagRequired("topic")
	kafkaCmd.AddCommand(UpdatePartitionReassignments)

	CreatePartitions.Flags().StringVarP(&ReplicaAssignment, "ReplicaAssignment", "r", "[]", "ReplicaAssignment: [[1,2],[0,1]]")
	CreatePartitions.Flags().IntVarP(&numPartition, "numPartition", "n", 0, "numPartition")
	CreatePartitions.Flags().StringSliceVarP(&topic, "topic", "t", []string{}, "kafka topic: t1,t2")
	_ = CreatePartitions.MarkFlagRequired("topic")
	_ = CreatePartitions.MarkFlagRequired("numPartition")
	kafkaCmd.AddCommand(CreatePartitions)

	kafkaCmd.AddCommand(expandPartitionsForAll)

	//DescribeConsumerGroup.Flags().StringVarP(&consumerGroup, "consumerGroup", "c", "[]", "consumerGroup")
	DescribeConsumerGroup.Flags().StringSliceVarP(&consumerGroup, "consumerGroup", "c", []string{}, "consumerGroup: cg1,cg2")
	kafkaCmd.AddCommand(DescribeConsumerGroup)

	//ConsumerTopic.Flags().Int64VarP(&beging, "beging", "b", -1, "offset -2 or ...")
	ConsumerTopic.Flags().BoolVarP(&frist, "frist", "f", false, "default OffsetNewest")
	ConsumerTopic.Flags().StringSliceVarP(&topic, "topic", "t", []string{}, "kafka topic: t1,t2")
	ConsumerTopic.Flags().StringVarP(&startTime, "startTime", "", "", "start time ( UTC )")
	_ = ConsumerTopic.MarkFlagRequired("topic")
	ConsumerTopic.MarkFlagsMutuallyExclusive("startTime", "frist")
	kafkaCmd.AddCommand(ConsumerTopic)

	CreateTopic.Flags().IntVarP(&numPartition, "numPartition", "n", 1, "numPartition")
	CreateTopic.Flags().IntVarP(&duplicate, "duplicate", "d", 1, "Replication Factor")
	CreateTopic.Flags().StringSliceVarP(&topic, "topic", "t", []string{}, "kafka topic: t1,t2")
	_ = CreateTopic.MarkFlagRequired("topic")
	kafkaCmd.AddCommand(CreateTopic)

	DeleteTopic.Flags().StringSliceVarP(&topic, "topic", "t", []string{}, "kafka topic: t1,t2")
	_ = DeleteTopic.MarkFlagRequired("topic")
	kafkaCmd.AddCommand(DeleteTopic)

	rootCmd.AddCommand(kafkaCmd)

}
